// 引入mongoose

const mongoose = require("mongoose");

// 链接数据库
mongoose
  .connect(
    "mongodb+srv://2048992672:gB4ndZgh8NVNPZoH@cluster0.ukhs7.mongodb.net/three-one"
  )
  .then(() => {
    console.log("数据库连接成功");
  })
  .catch((err) => {
    console.log("数据库连接失败", err);
});

const UserSchema = new mongoose.Schema({
    username: { type: String, unique: true, sparse: true },
    phone: { type: String, unique: true, sparse: true },
    password: { type: String, select: false },
    birthDate: Date,
    smsCode: {
      code: String,
      expiresAt: Date
    },
    refreshToken: { type: String, select: false },
    refreshTokenExpires: Date,
    createdAt: { type: Date, default: Date.now }
});
  
  
  
  
UserSchema.pre('save',async function(next){
    if(!this.isModified('password')) return next()
    const bcrypt=require('bcryptjs')
    this.password=await bcrypt.hash(this.password,10)
    next()
})


const User=mongoose.model('login',UserSchema,'login')
module.exports={
    User
}